package com.bawei.mall.product.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bawei.common.core.exception.ServiceException;
import com.bawei.mall.product.domain.*;
import com.bawei.mall.product.domain.model.*;
import com.bawei.mall.product.domain.request.ProductList;
import com.bawei.mall.product.domain.request.UserCollectList;
import com.bawei.mall.product.mapper.MallProductMapper;
import com.bawei.mall.product.service.MallProductService;
import com.bawei.mall.user.domain.MallUserFootprint;
import com.bawei.mall.user.domain.UserList;
import com.bawei.mall.user.domain.request.UserInformati;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

/**
 * @Author：张余庆
 * @Package：com.bawei.mall.product.service.impl
 * @Project：mall_cloud
 * @name：MallProductServiceImpl
 * @Date：2024/6/21 11:57
 */
@Service
public class MallProductServiceImpl implements MallProductService {

    @Autowired
    private MallProductMapper mapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询商品品牌列表
     * @return
     */
    @Override
    public MallList selBrandList() {
        MallList lis = new MallList();
        List<ProductBrand> list = mapper.selBrandList();
        List<MallAdvertiseInfo> lists = mapper.selGaoList();
        List<MallNewProduct> newProduct = mapper.selNewProduct();
        List<MallNewProduct> commentProduct = mapper.selCommentProduct();
        lis.setBrandList(list);
        lis.setAdvertiseList(lists);
        lis.setNewProductList(newProduct);
        lis.setHotProductList(commentProduct);
        return lis;
    }

    /**
     * 功能：根据品牌id查询商品
     * @param brandId
     * @return
     */
    @Override
    public MallList selProduct(Integer brandId,Integer pageNum,Integer pageSize) {
        MallList lis = new MallList();
        List<MallNewProduct> list = mapper.selProduct(brandId,pageNum,pageSize);
        lis.setList(list);
        return lis;
    }

    /**
     * 功能：根据id查询品牌
     * @param id
     * @return
     */
    @Override
    public ProductBrand selBrandById(Integer id) {
        ProductBrand list = mapper.selBrandById(id);
        return list;
    }

    /**
     * 功能：商品推荐广告查询
     * @return
     */
    @Override
    public MallList selGaoList() {
        MallList lis = new MallList();
        List<MallAdvertiseInfo> list = mapper.selGaoList();
        lis.setAdvertiseList(list);
        return lis;
    }

    /**
     * 功能：商品收藏
     * @param mallUserFollow
     */
    @Override
    public void add(MallUserFollow mallUserFollow) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        mallUserFollow.setUserId(userId);
        Integer i = mapper.add(mallUserFollow);
        if(i<=0){
            throw new RuntimeException("添加收藏说失败");
        }
    }

    /**
     * 功能：取消商品收藏
     * @param brandId
     */
    @Override
    public void delete(Integer brandId) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        Integer i = mapper.delete(brandId,userId);
        if(i<=0){
            throw new RuntimeException("取消失败");
        }
    }

    /**
     * 功能：查询用户地址
     * @return
     */
    @Override
    public List<MallUserAddress> userAddress() {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        List<MallUserAddress> list = mapper.userAddress(userId);
        return list;
    }

    /**
     * 功能：用户地址回显
     * @param id
     * @return
     */
    @Override
    public MallUserAddress address(Integer id) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        MallUserAddress list = mapper.address(id,userId);
        return list;
    }

    /**
     * 功能：编辑用户地址
     * @param address
     */
    @Override
    public void updateAddress(MallUserAddress address) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        address.setUserId(userId);
        Integer i = mapper.updateAddress(address);
        if(i<=0){
            throw new RuntimeException("编辑失败");
        }
    }

    /**
     * 功能：删除用户地址
     * @param id
     */
    @Override
    public void delAddress(Integer id) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        Integer i = mapper.delAddress(userId,id);
        if(i<=0){
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 功能：添加用户地址
     * @param address
     */
    @Override
    public void addAddress(MallUserAddress address) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        address.setUserId(userId);
        Integer i = mapper.addAddress(address);
        if(i<=0){
            throw new RuntimeException("添加失败");
        }
    }

    /**
     * 功能：添加广告表
     * @param advertiseInfo
     */
    @Override
    public void addAdvertise(MallAdvertiseInfo advertiseInfo) {
        Integer i = mapper.addAdvertise(advertiseInfo);
        if(i<=0){
            throw new RuntimeException("添加失败");
        }
    }

    /**
     * 功能：修改广告表
     * @param advertiseInfo
     */
    @Override
    public void updateAdvertise(MallAdvertiseInfo advertiseInfo) {
        Integer i = mapper.updateAdvertise(advertiseInfo);
        if(i<=0){
            throw new RuntimeException("修改失败");
        }
    }

    /**
     * 功能：删除广告
     * @param id
     */
    @Override
    public void delAdvertise(Integer id) {
        Integer i = mapper.delAdvertise(id);
        if(i<=0){
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 功能：查询广告
     * @param id
     * @return
     */
    @Override
    public MallAdvertiseInfo selAdvertise(Integer id) {
        MallAdvertiseInfo advertiseInfo = mapper.selAdvertise(id);
        return advertiseInfo;
    }

    /**
     * 功能：查询新品
     * @return
     */
    @Override
    public List<MallNewProduct> selNewProduct() {
        List<MallNewProduct> newProduct = mapper.selNewProduct();
        return newProduct;
    }

    /**
     * 功能：查询推荐商品
     * @return
     */
    @Override
    public List<MallNewProduct> selCommentProduct() {
        List<MallNewProduct> commentProduct = mapper.selCommentProduct();
        return commentProduct;
    }

    /**
     * 功能：查询用户足迹表
     * @return
     */
    @Override
    public UserList list(Integer pageNum, Integer pageSize) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        UserList userList = new UserList();
        List<MallUserFootprint> list = mapper.list(userId,pageSize,pageNum);
        userList.setList(list);
        return userList;
    }

    /**
     * 功能：删除用户足迹
     */
    @Override
    public void delFootprint() {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        Integer i = mapper.delFootprint(userId);
        if(i<=0){
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 功能：添加用户足迹
     * @param mallUserFootprint
     */
    @Override
    public void addFootprint(MallUserFootprint mallUserFootprint) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        mallUserFootprint.setUserId(userId);
        Integer i = mapper.addFootprint(mallUserFootprint);
        if(i<=0){
            throw new RuntimeException("添加失败");
        }
    }

    /**
     * 功能：根据ID查询商品 和品牌
     * @param id
     * @return
     */
    @Override
    public ProductList selProductById(Integer id) {
        ProductList list = new ProductList();
        // 根据id查询商品
        MallNewProduct productlist = mapper.selProductById(id);
        // 获取品牌id
        Integer brandId = productlist.getBrandId();
        // 根据商品id查询品牌
        ProductBrand brandlist = mapper.selBrandBbrandId(brandId);
        // 获取商品 商品属性分类ID
        Integer productAttributeCategoryId = productlist.getProductAttributeCategoryId();
        // 根据商品属性分类ID 查询商品属性
        List<MallProductAttriSku> attriSkus = mapper.getAattriSkus(productAttributeCategoryId);
        Integer skusId = productlist.getId();
        List<MallProductValue> values = mapper.getValueList(skusId);
        List<MallProductStock> stockList = mapper.getStockList(skusId);
        List<MallProductPrice> priceList = mapper.getPriceList(skusId);
        list.setProduct(productlist);
        list.setBrand(brandlist);
        list.setProductAttributeList(attriSkus);
        list.setProductAttributeValueList(values);
        list.setSkuStockList(stockList);
        list.setProductLadderList(priceList);
        return list;
    }

    /**
     * 功能：根据关键字查询商品
     * @return
     */
    @Override
    public MallList selProductList(String keywords,Integer pageNum,Integer pageSize) {
        if(keywords==null){
            throw new RuntimeException("关键字不能为空");
        }
        MallList malllist = new MallList();
        List<MallNewProduct> list = mapper.selProductList(keywords,pageSize,pageNum);
        malllist.setHotProductList(list);
        return malllist;
    }


    /**
     * 功能：查询用户关注表
     * @return
     */
    @Override
    public UserFollowList getUserFollow(Integer pageNum,Integer pageSize) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        UserFollowList list = new UserFollowList();
        List<MallUserFollow> mallUserFollow = mapper.getUserFollow(userId,pageNum,pageSize);
        list.setList(mallUserFollow);
        return list;
    }

    /**
     * 功能：清空用户关注
     */
    @Override
    public void clear() {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        Integer i = mapper.clear(userId);
        if(i<=0){
            throw new RuntimeException("清空失败");
        }
    }
}
